package com.itjason.utils;

import com.alibaba.fastjson.JSON;
import com.itjason.exceptionhandler.CustomException;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class TencentCOSFileUpload {
    private static String SecretKey;

    private static String SecretId;

    private static String BucketName;

    private static String BseURL;

    public static String BseURL01;


    @Value("${cos.baseUrl}")
    public void setBseURL(String bseURL) {
        BseURL = bseURL;
    }

    @Value("${cos.baseUrl}")
    public void setBseURL01(String bseURL) {
        BseURL01 = bseURL;
    }


    @Value("${cos.secretKey}")
    public void setSecretKey(String secretKey) {
        SecretKey = secretKey;
    }


    @Value("${cos.secretId}")
    public void setSecretId(String secretId) {
        SecretId = secretId;
    }


    @Value("${cos.bucketName}")
    public void setBucketName(String bucketName) {
        BucketName = bucketName;
    }

    public static COSClient createCosClient() {
        System.out.println(SecretId);
        // 1 初始化用户身份信息（secretId, secretKey）。
        // SECRETID和SECRETKEY请登录访问管理控制台 https://console.cloud.tencent.com/cam/capi 进行查看和管理
        String secretId = SecretId;
        String secretKey = SecretKey;
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // 2 设置 bucket 的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        // clientConfig 中包含了设置 region, https(默认 http), 超时, 代理等 set 方法, 使用可参见源码或者常见问题 Java SDK 部分。
        Region region = new Region("ap-nanjing");
        ClientConfig clientConfig = new ClientConfig(region);
        // 这里建议设置使用 https 协议
        // 从 5.6.54 版本开始，默认使用了 https
        clientConfig.setHttpProtocol(HttpProtocol.https);
        // 3 生成 cos 客户端。
        COSClient cosClient = new COSClient(cred, clientConfig);
        return cosClient;
    }

    public static String uploadfile(File file, String newFileName, String folderPrefix) {
        try {
            // 指定文件上传到 COS 上的路径，即对象键。例如对象键为folder/picture.jpg，则表示将文件 picture.jpg 上传到 folder 路径下
            String key = folderPrefix + newFileName;
            PutObjectRequest putObjectRequest = new PutObjectRequest(BucketName, key, file);
            PutObjectResult putObjectResult = createCosClient().putObject(putObjectRequest);
            System.out.println(JSON.toJSONString(putObjectResult));
            createCosClient().shutdown();
            return BseURL + folderPrefix + newFileName;
        } catch (Exception clientException) {
            clientException.printStackTrace();
            throw new CustomException("500", "添加失败！");
        }
    }

    public static void deletefile(String filename, String folderPrefix) {
        //对象键
        String key = folderPrefix + filename;
        DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(BucketName, key);
        createCosClient().deleteObject(deleteObjectRequest);
    }
}
